package com.tdkj.task;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Table;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tdkj.model.user.BeanUser;
import com.tdkj.model.user.BeanUserLevel;
import com.tdkj.service.user.IBeanUserLevelService;

public class UserLevelTask {
    private static final Logger logger = LoggerFactory.getLogger(UserLevelTask.class);
    
    private IBeanUserLevelService userLevelService;
 
    @Resource
	public void setUserLevelService(IBeanUserLevelService userLevelService) {
		this.userLevelService = userLevelService;
	}

	public void execute() {
    	while(true){
    		Long sleepMillis = 2L*60*1000;
    			
    		String sql = " select t1.id as user_id from "+BeanUser.class.getAnnotation(Table.class).name()+" t1 left join "+BeanUserLevel.class.getAnnotation(Table.class).name()+" t2 on (t1.user_level_id=t2.id) where t2.id is null or (t1.experience>t2.endnumber or t1.experience<t2.startnumber)";
    		List<Object> userIdObjects = userLevelService.findBySql(sql);
    		
			for(Object userId : userIdObjects){
				userLevelService.updateUser(((BigInteger)userId).longValue());
			}
			
    		try {
    			//睡眠时间
				Thread.sleep(sleepMillis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    	}
    }
}
